import random

import pymysql


def mydb(sql):
    """数据库
    param text:传入sql语句
    :return data:  返回一个元组
    """
    db = pymysql.connect(user='root',
                         password='123456',
                         host='localhost',
                         port=3306,
                         database='userinfo',
                         charset='utf8mb4')
    cs = db.cursor()
    cs.execute(sql)
    data = cs.fetchall()
    db.commit()
    db.close()
    return data


print("欢迎来到背诵抽题小程序")
data = mydb(f"SELECT DISTINCT tag, qname,qid from question_sam")
res = random.sample(data, 3)
print(res)

a = b = c = list(data)
subject = []
# while len(subject) <= 10:
for i in range(5):
    if len(a) == 0:
        break
    next_question = random.choice(a)
    a.remove(next_question)
    subject.append(next_question)
for j in range(3):
    if len(b) == 0:
        break
    next_question = random.choice(b)
    b.remove(next_question)
    subject.append(next_question)
for x in range(2):
    if len(c) == 0:
        break
    next_question = random.choice(c)
    c.remove(next_question)
    subject.append(next_question)

    total = a + b + c

print(subject)










